<%@ page import="java.io.* , java.util.*, java.sql.*, com.myapp.struts.dao.*,com.myapp.struts.actionform.*,com.myapp.struts.beans.*"%>
<html>
    <!-- DW6 -->
    <head>
        <!-- Copyright 2005 Macromedia, Inc. All rights reserved. -->
        <title>PURCHASE ENTRY</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <link href="../../../../css/cms.css" rel="stylesheet" type="text/css"> 
        <script language="JavaScript" type="text/javascript">
            var x=0;
            function validate()
            {
                if(document.getElementById("pid").value =="")
                {
                    alert("Please enter the Purchase Number ");
                    document.getElementById("pid").focus();
                    return false;
                }
                var index1=document.purchase_from.siteCode.selectedIndex;
                if(index1==0)
                {
                    alert("Please Make a Selection from Site Code");
                    document.purchase_from.siteCode.focus();
                    return false;
                }
                var ind1=document.purchase_from.daydropdown.selectedIndex;
				
                if(ind1==0)
                {
                    alert("SELECT DAY!..........");
                    document.purchase_from.daydropdown.focus();
                    return false;
                }
				var ind2=document.purchase_from.monthdropdown.selectedIndex;
				
				if(ind2==0)
                {

                    alert("SELECT MONTH!.........");
                    document.purchase_from.monthdropdown.focus();
                    return false;
                }
				var ind3=document.purchase_from.yeardropdown.selectedIndex;				
				if(ind3==0)
                {
                    alert("SELECT YEAR!.........");
                    document.purchase_from.yeardropdown.focus();
                    return false;
                }
				var index1=document.purchase_from.sname.selectedIndex;
                if(index1==0)
                {
                    alert("SELECT SUPPLIER!........");
                    document.purchase_from.sname.focus();
                    return false;
                }
				

                <!---->
                if (document.purchase_from.itemCode.length>0)
                {
                    for (var i=0;i<document.purchase_from.itemCode.length;i++)
                    {
                        if (i==0)
                        {
                            <!--if i==0 then validate this -->
                            var index1=document.purchase_from.itemCode.selectedIndex;
                            if(index1<=0)
                            {
                                alert("Please Make a Selection from Item Name");
                                document.purchase_from.itemCode.focus();
                                return false;
                            }
                            if(document.purchase_from.qty.value == "0.0")
                            {
                                x++;
                                alert("Please Enter a qty....");
                                document.purchase_from.qty.focus();
                                return false;
                            }
                            if(document.purchase_from.rate.value == "0.0")
                            {
                                x++;
                                alert("Please Enter a Rate....");
                                document.purchase_from.rate.focus();
                                return false;
                            }
                        }else if(i>0)
                        {
                            var index1=document.purchase_from.itemCode[i].selectedIndex;
                            if(index1<=0)
                            {
                                alert("Please Make a Selection from Item Name");
                                document.purchase_from.itemCode[i].focus();
                                return false;
                            }
                            if(document.purchase_from.qty[i].value == "0.0")
                            {
                                x++;
                                alert("Please Enter a qty....");
                                document.purchase_from.qty[i].focus();
                                return false;
                            }
                            if(document.purchase_from.rate[i].value == "0.0")
                            {
                                x++;
                                alert("Please Enter a Rate....");
                                document.purchase_from.rate[i].focus();
                                
                                return false;
                            }
                        }
                    }
                }
            }
        </script>
        <script language="javascript">
            function tabEvent(eve, ccount){
                var count = document.getElementById("optionCount").value;
                var keynum;
                if(window.event) // IE
                    keynum = eve.keyCode;
                else if(eve.which) // Netscape/Firefox/Opera
                    keynum = eve.which;
                if(keynum == 9 && ccount == count){
                    count = parseInt(count) + 1;
                    document.getElementById("optionCount").value = count;
                    var appendStr = '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td><select name="itemCode" id="itemCode'+count+'" style="width:200" onChange="javascript:getItem(this.id)"></select></td><td><input type="text" name="qty" id="qty'+count+'" value="0.0" size="10"></td><td><input type="text" name="rate" id="rate'+count+'" value="0.0" size="10"></td><td><input type="text" name="tax" id="tax'+count+'" value="0.0" size="10"></td><td><input type="text" name="ed" id="ed'+count+'" value="0.0" size="10"></td><td><input type="text" name="sce" id="sce'+count+'" value="0.0" size="10"></td><td><input type="text" name="fi" id="fi'+count+'" value="0.0" size="10"></td><td><input type="text" name="vat" id="vat'+count+'" value="0.0" size="10"></td><td><input type="text" name="dis"  id="dis'+count+'" value="0.0" size="10" onkeydown="javascript:tabEvent(event,'+count+');"  ></td></tr><br></table>';
                    document.getElementById('rrows').innerHTML = document.getElementById('rrows').innerHTML + appendStr;
                    var eleIds = "itemCode"+count;
                    fillOptions(eleIds);
                }
            }

            function fillOptions(eleId)
            {
                var elemnt = document.getElementById(eleId);
                var actualEle = document.getElementById("itemCode");
                for(i=0;i<actualEle.length;i++){
                    addOption(elemnt,actualEle[i].text,actualEle[i].text);
                }
            }

            function addOption(selectbox,text,value )
            {
                var optn = document.createElement("OPTION");
                optn.text = text;
                optn.value = value;
                selectbox.options.add(optn);
            }
        </script>
        <script type="text/javascript">

            function getXMLObject()  //XML OBJECT
            {
                var xmlHttp = false;
                try {
                    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
                }
                catch (e) {
                    try {
                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
                    }
                    catch (e2) {
                        xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
                    }
                }
                if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
                    xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
                }
                return xmlHttp;  // Mandatory Statement returning the ajax object created
            }

            var xmlhttp = new getXMLObject();	//xmlhttp holds the ajax object

            function getItem(id)
            {
   
                var item_code=document.getElementById(id).value;
                if(xmlhttp) {
                    xmlhttp.open("POST","../../../../AJAXItemName?icode=" +item_code,true);
                    xmlhttp.onreadystatechange  = handleServerResponse;
                    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                    xmlhttp.send(null);
                }
                <!--document.getElementById("one").innerHTML=item_code;-->
            }
            function handleServerResponse() {
                if (xmlhttp.readyState == 4) {
                    if(xmlhttp.status == 200) {
                        var textToSplit = xmlhttp.responseText;
                        var returnElements=textToSplit.split("|");
                        var valueLabelPair = returnElements[0].split("|");
                        document.getElementById("one").innerHTML=valueLabelPair[0];
                    }else {
                        alert("Error during AJAX call. Please try again");
                    }
                }
            }
        </script>
        <script type="text/javascript">
            <!--
            function MM_reloadPage(init) {  //reloads the window if Nav4 resized
                if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
                        document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
                else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
            }
            MM_reloadPage(true);
            //-->
        </script>
        <script type="text/javascript">
		
            var monthtext=[1,2,3,4,5,6,7,8,9,10,11,12];

            function populatedropdown(dayfield, monthfield, yearfield){
                var today=new Date()
                var dayfield=document.getElementById(dayfield)
                var monthfield=document.getElementById(monthfield)
                var yearfield=document.getElementById(yearfield)
                for (var i=0; i<31; i++)
                    dayfield.options[i]=new Option(i, i+1)
                dayfield.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true) //select today's day
                for (var m=0; m<12; m++)
                    monthfield.options[m]=new Option(monthtext[m], monthtext[m])
                monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true) //select today's month
                var thisyear=today.getFullYear()
				yearfield.options[0]=new Option("", "")
                for (var y=1; y<=20; y++){
                    yearfield.options[y]=new Option(thisyear, thisyear)
                    thisyear+=1
                }
                yearfield.options[1]=new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year
            }

        </script>
    </head>
    <body>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td height="546" align="center"><br><br>
                    <form id="purchase_from" name="purchase_from"  method="post" action="../../../../InsertPurchaseEntry" onsubmit ="return validate()">
                        <table width="617" border="0" align="center" cellpadding="0" cellspacing="0" style="border-right: #000000 1px solid;
                               border-top: #000000 1px solid; border-left: #000000 1px solid;  border-bottom: #000000 1px solid; font-size: 9pt; color: #000000; font-family: Arial;">
                            <tr>
                                <th height="31" colspan="8" align="center" valign="middle">Purchase Entry</th>
                            </tr>
                            <tr>
                                <td width="148" align="left" valign="middle" nowrap="nowrap"><span class="style3 style5">Purchase ID</span></td>
                          <td valign="middle"><input name="pid" type="text" id="pid" /></td>
                                <td valign="middle">Site Code</td>
                                <td align="left" valign="middle" nowrap="nowrap"><select name="siteCode" id="siteCode" onChange="javascript:getLocationDetails()">
                                        <option> </option>
                                        <%
                              LocationMasterDao lmdao = new LocationMasterDao();
                              Vector lvc = lmdao.getLocations();
                              if (lvc != null) {
                                  Iterator itg = lvc.iterator();
                                  while (itg.hasNext()) {
                                      LocationMasterFormBean gb = (LocationMasterFormBean) itg.next();
                                      String pname = gb.getLocationCode();
                                        %>
                                        <option value="<%=pname%>"><%=pname%></option>
                                        <%}

                              }%>
                                    </select></td>
                            </tr>
                            <tr>
                                <td align="left" valign="middle" ><span class="style1 style5">Purchase Date</span><span class="style5">
                                        <label></label>
                                    </span></td>
                                <td align="left" valign="middle" nowrap="nowrap">
                                	<select id="daydropdown" name="daydropdown">
                                    <option></option>
                                    </select>
                                    <select id="monthdropdown" name="monthdropdown">
                                    <option></option>
                                    </select>
                                    <select id="yeardropdown" name="yeardropdown">
                                    <option></option>
                                    </select>

                                    <script type="text/javascript">

                                        //populatedropdown(id_of_day_select, id_of_month_select, id_of_year_select)
                                        window.onload=function(){
                                            populatedropdown("daydropdown", "monthdropdown", "yeardropdown")
                                        }
                                    </script></td>
                                <td align="left" valign="middle" nowrap="nowrap">&nbsp;</td>
                                <td align="left" valign="middle" nowrap="nowrap">&nbsp;</td>
                            </tr>
                            <tr>
                                <td align="left" valign="middle">Supplier Name</td>
                                <td width="152" align="left" valign="middle"><select name="sname" id="sname" onChange="javascript:getDetails()">
                                        <option> </option>
                                        <%
                                                      SupplierDao sdao=new SupplierDao();
                                                      Vector gp1 = sdao.getSupplierNames();
                              if (gp1 != null) {
                                  Iterator itg = gp1.iterator();
                                  while (itg.hasNext()) {
                                      SupplierMasterFormBean gb = (SupplierMasterFormBean) itg.next();
                                      String supp_name = gb.getSupplierName();
                                        %>
                                        <option value="<%=supp_name%>"><%=supp_name%></option>
                                        <%}

                              }%>
                                    </select></td>
                              <td width="152" align="left" valign="middle"  class="style9">Selected Item</td>
                              <td width="163" align="left" valign="middle" ><div id="one" class="style9"></div></td>
                          </tr>

                            <tr>
                                <th  height="26" colspan="8" align="left" valign="middle" nowrap="nowrap">
                                    <div id="rrows">
                                        <table  border="0" cellpadding="0" cellspacing="0" style="border-right: #000000 1px solid;
                               border-top: #000000 1px solid; border-left: #000000 1px solid;  border-bottom: #000000 1px solid; font-size: 9pt; color: #000000; font-family: Arial;">
                                            <tr>
                                                <th width="218" height="21" align="center">Item Code</th>
                                                <th width="76"  align="right">QTY</th>
                                                <th width="75"  align="right">Rate(%)</th>
                                                <th width="79"  align="right">Tax(%)</th>
                                                <th width="90"  align="right">ED(%)</th>
                                                <th width="79"  align="right">S.cess(%)</th>
                                                <th width="84"  align="right">F.I(Rs)</th>
                                                <th width="80"  align="right">Vat(%)</th>
                                                <th width="87"  align="right">Discount(%)</th>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <input type="hidden" name="optionCount" id="optionCount"  value="0">
                                                    <select name='itemCode' id='itemCode' style="width:200" onChange="javascript:getItem(this.id)">
                                                        <option>Select</option>
                                                        <%MaterialMasterDao imdao = new MaterialMasterDao();Vector gp = imdao.getItemCodes();if (gp != null) {Iterator itg = gp.iterator();while (itg.hasNext()) {ItemBean gb = (ItemBean) itg.next();String pname = gb.getItemname();%><option value="<%=pname%>"><%=pname%></option><%}}%>
                                                    </select>    </td>
                                                <td ><input name='qty' type='text' id='qty' value="0.0" size="10" /></td>
                                                <td ><input name='rate' type='text' id='rate' value="0.0" size="10"/></td>
                                                <td ><input name='tax' type='text' id='tax' value="0.0" size="10" /></td>
                                                <td ><input name='ed' type='text' id='ed' value="0.0" size="10"/></td>
                                                <td ><input name='sce' type='text' id='sce' value="0.0" size="10"/></td>
                                                <td ><input name="fi" type="text" id="fi" value="0.0" size="10" /></td>
                                                <td ><input name="vat" type="text" id="vat" value="0.0" size="10" /></td>
                                                <td ><input name='dis' type='text' id='dis' onKeyDown="javascript:tabEvent(event, 0);" value="0.0" size="10"/></td>
                                            </tr>
                                        </table>
                                    </div></th>
                            </tr>
                            <tr>
                                <td height="24" colspan="8" align="center" valign="middle" >
                                    <input type="submit" name="Submit" value="Submit">                </td>
                            </tr>
                        </table>
                    </form>
                    <script language="javascript">
                        document.getElementById("optionCount").value=0;
                    </script>
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
            </tr>
        </table>



    </body>
</html>
